package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckitemService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckitemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckitemService {
    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${out_put_path}")
    private String outPutPath;

    @Autowired
    private CheckItemDao checkItemDao;

    //添加检查项
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
       // StaticHtml.generateMobileStaticHtml();
        generateMobileStaticHtml();
    }

    //检查项分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //完成分页查询，他是基于mybatis提供的分页助手来完成的
        PageHelper.startPage(currentPage, pageSize);

        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        PageResult pageResult = new PageResult(total, rows);
        return pageResult;
    }

    @Override
    public long findCountById(Integer id) {
        long count = checkItemDao.findCountById(id);
        return count;
    }

    @Override
    public CheckItem findById(Integer id) {

        CheckItem checkItem=checkItemDao.findById(id);
        return checkItem;
    }

    //编辑检查项
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
       // StaticHtml.generateMobileStaticHtml();
        generateMobileStaticHtml();
    }

    //删除检查项
    @Override
    public void delById(Integer integer) {
        checkItemDao.delById(integer);
       // StaticHtml.generateMobileStaticHtml();
        generateMobileStaticHtml();
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList=checkItemDao.findAll();
        return checkItemList;
    }




    public  void generateMobileStaticHtml(){

        //生成静态页面之前查询所有数据
        List<Setmeal> setmeals = setmealDao.getSetmeal();

        //生成套餐列表页面
        generateMobileSetmealListHtml(setmeals);

        //生成套餐详情页面
        generateMobileSetmealDetailHtml(setmeals);


    }

    //生成套餐列表页面
    public  void generateMobileSetmealListHtml(List<Setmeal> setmeals){
        Map map = new HashMap();
        map.put("setmealList",setmeals);
        generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }
    //生成套餐详情页面
    public  void generateMobileSetmealDetailHtml(List<Setmeal> setmeals){
        for (Setmeal setmeal : setmeals) {
            Map map = new HashMap();
            map.put("setmeal",setmealDao.findById(setmeal.getId()));
            generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }

    }

    //抽取方法，用于生成静态页面
    public  void generateHtml(String templateName,String htmlPageName,Map map){
        //获取配置对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        //加载模板文件
        Writer writer = null;
        try {
            Template template = configuration.getTemplate(templateName);
            //创建输出流，输出静态文件
            // writer=new FileWriter(new File(outPutPath+"/"+htmlPageName));
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(outPutPath + "/" + htmlPageName),"utf-8");
            //输出文件
            template.process(map,outputStreamWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
